package com.xbdx.lyboot.util;

import com.xbdx.lyboot.handller.MailHandler;
import org.springframework.beans.factory.annotation.Autowired;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

public class MailUtil {

    public static void send() throws Exception {
        Properties prop=new Properties();
        prop.setProperty("mail.transport.protocol","smtp");
        prop.setProperty("mail.smtp.host","smtp.qq.com");
        prop.setProperty("mail.smtp.auth","true");

        String port = "465";
        prop.setProperty("mail.smtp.port",port);
        prop.setProperty("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        prop.setProperty("mail.smtp.socketFactory.fallback","false");
        prop.setProperty("mail.smtp.socketFactory.port",port);

        // 2) 创建会话
        Session session = Session.getInstance(prop);

        //创建一封邮件
        MimeMessage message=new MimeMessage(session);
        String sendMail ="1300893863@qq.com";
        String Recipients ="2234352912@qq.com";
        message.setFrom(new InternetAddress(sendMail,"李勇","UTF-8"));
        message.setRecipient(MimeMessage.RecipientType.TO ,
                new InternetAddress(Recipients,"勇帅","UTF-8"));

        //标题  正文 发件时间
        message.setSubject("来自520的密切问候","UTF-8");
        message.setContent("520呵呵哒，快乐！","text/html;charset=UTF-8");
        message.setSentDate(new Date());


        // 可以保存为 *.eml的文件格式
        message.saveChanges();

        //4/获取邮件传输对象 ，简历连接并发送
        Transport transport=session.getTransport();
        String account="1300893863@qq.com";
        String password="mkbfyomjvlcyjfeg";
        transport.connect(account,password);

        transport.sendMessage(message,message.getAllRecipients());

        transport.close();
    }

    public static void main(String[] args) throws Exception {
        
        send();
    }
}
